package practise.alogrithm.hundredInterviewQuestions;

import java.lang.annotation.ElementType;
import java.util.Stack;

/**
 * reverse A stack, cannot use while and for loop, just use recursive method.
 */
public class ReverseStack<T> {
    public <T> Stack<T> reverseStact(Stack<T> stack) {
        if (stack.isEmpty()) {
            return stack;
        }
        T element = stack.pop();
        Stack<T> stackResult = reverseStact(stack);
        putElementToEnd(stackResult, element);

        return stack;

    }

    public <T> Stack<T> putElementToEnd(Stack<T> stack, T element) {
        if (stack.isEmpty()) {
            stack.push(element);
            return stack;
        }
        T t = stack.pop();
        putElementToEnd(stack, element);
        stack.push(t);
        return stack;

    }

    public static void main(String[] args) {
        ReverseStack<Integer> reverseStack = new ReverseStack<>();
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 6; i++) {
            stack.push(i);
        }
        reverseStack.reverseStact(stack);
        System.out.println("Result:" + stack);
    }
}
